<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\home\controller;

/**
 * Description of ApiCommon
 *
 * @author Administrator
 */
use think\Request;
use think\Db;
class ApicommonController extends CommonController{
    public $userinfo = null;
    public function _initialize()
    {
        parent::_initialize();
//        echo 'dsafd';die;
        /*获取头部信息*/ 
        $header = Request::instance()->header();
        
        $authKey = $header['authkey'];
        $sessionId = $header['sessionid'];
        $cache = cache('Auth_'.$authKey);
        // 校验sessionid和authKey
        if (empty($sessionId)||empty($authKey)||empty($cache)) {
            header('Content-Type:application/json; charset=utf-8');
            exit(json_encode(['code'=>0, 'error'=>'登录已失效']));
        }

        // 检查账号有效性
        $fanwe = db('user');
        $userInfo = $cache['userInfo'];
        $map['id'] = $userInfo['user_id'];
        $map['is_delete'] = 0;    
        $map['is_effect'] = 1;
        if (!$user = $fanwe->where($map)->find()) {
            
            header('Content-Type:application/json; charset=utf-8');
            return json ([['code'=>0,'msg'=>'账户被禁用或已删除','data'=>[],'jump'=>'login']]);
            exit;
        }
        $sql = 'select a.user_id,a.supplier_id,a.is_delete,a.is_main,s.name as supplier_name,s.preview,s.address,s.user_id as supplier_userid,s.type from fanwe_supplier_account as a inner join fanwe_supplier as s on s.id = a.supplier_id where a.user_id = '.$user['id'].' limit 0,1';
        $seller = $fanwe->query($sql)[0];
        if(empty($seller)){
            return json ([['code'=>0,'msg'=>'请使用商户账户或子账户登录','data'=>[],'jump'=>'login']]);
            exit;
        }
        // 更新缓存
        cache('Auth_'.$authKey, $cache, config('LOGIN_SESSION_VALID'));
        $this->userinfo = $userInfo;
    }
}
